/**
 * 
 */
package com.manthano.util.file;

import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
 * @author DELL
 */
public class XLSXFileReader implements FileReader
{

	private Sheet			sheet			= null;
	private Workbook		workbook		= null;
	private int				rowIndex		= 0;
	private int				columncount		= 0;
	private NumberFormat	numberFormat	= NumberFormat.getNumberInstance();

	/**
	 * </pre>
	 * 
	 * @param inputStream
	 * @throws IOException
	 */
	public XLSXFileReader(InputStream inputStream) throws IOException
	{
		try
		{
			workbook = WorkbookFactory.create(inputStream);
			sheet = workbook.getSheetAt(0);
		}
		catch (InvalidFormatException e)
		{
			throw new IOException(e);
		}
	}

	public void close() throws IOException
	{
		if (workbook != null)
		{
			// workbook.close();
			workbook = null;
		}
	}

	public String[] readNext() throws IOException
	{
		String[] cellData = null;
		Row row = sheet.getRow(rowIndex);
		if (rowIndex == sheet.getPhysicalNumberOfRows())
		{
			return null;
		}
		else if (rowIndex == 0)
		{
			columncount = row.getLastCellNum();
		}

		cellData = new String[columncount];

		for (int column = 0; column < columncount; column++)
		{
			Cell cell = row.getCell(column);
			if ((cell == null) || (cell.getCellType() == Cell.CELL_TYPE_BLANK))
			{
				cellData[column] = " ";
				continue;
			}
			switch (cell.getCellType())
			{
				case Cell.CELL_TYPE_BLANK:
					cellData[column] = " ";
					break;
				case Cell.CELL_TYPE_STRING:
					String cellValue = cell.getRichStringCellValue().getString();
					if (cellValue.isEmpty())
					{
						cellData[column] = " ";
					}
					else
					{
						cellData[column] = cellValue;
					}
					break;
				case Cell.CELL_TYPE_NUMERIC:
					if (DateUtil.isCellDateFormatted(cell))
					{
						cellData[column] = String.valueOf(cell.getDateCellValue());
					}
					else
					{
						numberFormat.setGroupingUsed(false);
						numberFormat.setMaximumFractionDigits(8);
						cellData[column] = numberFormat.format(cell.getNumericCellValue());
					}
					if (cellData[column].endsWith(".0"))
					{
						cellData[column] = cellData[column].substring(0, cellData[column].length() - 2);
					}
					break;
				case Cell.CELL_TYPE_BOOLEAN:
					cellData[column] = String.valueOf(cell.getBooleanCellValue());
					break;
				case Cell.CELL_TYPE_FORMULA:
					cellData[column] = String.valueOf(cell.getCellFormula());
					break;
				case Cell.CELL_TYPE_ERROR:
					cellData[column] = String.valueOf("Cell value is error" + cell.getErrorCellValue());
					break;
				default:
					break;
			}
		}
		rowIndex++;
		return cellData;
	}
}
